home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / Apache 1.0 / support / inc2shtml.c < prev    next >
C/C++ Source or Header  |  1995-12-04  |  3KB  |  106 lines

  1. /*
  2.  * inc2shtml: Convert httpd <1.1 style includes to 1.2 style
  3.  * 
  4.  * Rob McCool
  5.  * 
  6.  * Usage: inc2shtml [filename]
  7.  * 
  8.  * If filename is given, this program will open filename. If not, it will 
  9.  * look on stdin. It will output the new shtml file on stdout.
  10.  */
  11.  
  12.  
  13. #include <stdio.h>
  14. #ifdef sony_mips_bsd
  15. #include <ctype.h>
  16. #endif
  17.  
  18. #define MAX_STRING_LEN 256
  19.  
  20. void usage(char *argv0) {
  21.     fprintf(stderr,"Usage: %s [filename]\n",argv0);
  22.     fprintf(stderr,"If filename is given, this program will open filename.\n");
  23.     fprintf(stderr,"If not, it will look on stdin for the inc file.\n");
  24.     fprintf(stderr,
  25.             "In either case, it will write the new shtml file on stdout.\n");
  26.     exit(1);
  27. }
  28.  
  29. void translate_tag(char *tag, FILE *fd) {
  30.     char *tp = tag, *tp2;
  31.     int url;
  32.  
  33.     url = (*tp == 'U' || *tp == 'u' ? 1 : 0);
  34.         
  35.     while(*tp++ != '\"');
  36.     tp2 = tp + 1;
  37.     while(*tp2 != '\"') ++tp2;
  38.     *tp2 = '\0';
  39.     if(*tp == '|') {
  40.         fprintf(fd,"<!--#exec cmd=\"%s",++tp);
  41.         if(url) fputs(" '$QUERY_STRING_UNESCAPED'",fd);
  42.         fputs("\"-->",fd);
  43.     } else
  44.         fprintf(fd,"<!--#include virtual=\"%s\"-->",tp);
  45. }
  46.  
  47. main(int argc, char *argv[]) {
  48.     FILE *f;
  49.     int c,x,p;
  50.     char c2;
  51.     char *lookfor = "<inc srv";
  52.  
  53.     switch(argc) {
  54.       case 1:
  55.         f = stdin;
  56.         break;
  57.       case 2:
  58.         if(!(f = fopen(argv[1],"r"))) {
  59.             perror("fopen");
  60.             exit(1);
  61.         }
  62.         break;
  63.       default:
  64.         usage(argv[0]);
  65.     }
  66.  
  67.     p=0;
  68.     while(1) {
  69.         c = fgetc(f);
  70.         if(c == -1) {
  71.             fflush(stdout);
  72.             exit(0);
  73.         }
  74.         c2 = (char)c;
  75.         if(isalpha((char)c))
  76.             c = tolower((char)c);
  77.         if(c == lookfor[p]) {
  78.             if(!lookfor[++p]) {
  79.                 char tag[MAX_STRING_LEN];
  80.  
  81.                 x=0;
  82.                 c = fgetc(f); /* get space */
  83.                 while(c != '>') {
  84.                     tag[x++] = c;
  85.                     c = fgetc(f);
  86.                     if(c == -1) {
  87.                         fputs("<inc srv ",stdout);
  88.                         fputs(tag,stdout);
  89.                         fflush(stdout);
  90.                         exit(1);
  91.                     }
  92.                 }
  93.                 tag[x] = '\0';
  94.                 translate_tag(tag,stdout);
  95.                 p = 0;
  96.             }
  97.         } 
  98.         else {
  99.             for(x=0;x<p;x++)
  100.                 fputc(lookfor[x],stdout);
  101.             fputc(c2,stdout);
  102.             p=0;
  103.         }
  104.     }
  105. }
  106.